package com.jingbaoduo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jingbaoduo.common.BaseContext;
import com.jingbaoduo.common.R;
import com.jingbaoduo.domain.Goods;
import com.jingbaoduo.domain.ShoppingCart;
import com.jingbaoduo.service.GoodsService;
import com.jingbaoduo.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/shoppingcart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车中的数据:{}",shoppingCart);
        //设置用户ld
        Long currentId = BaseContext.getCurrentId();
        log.info("当前用户id:{}",currentId);
        shoppingCart.setUserId(currentId);
        //设置创建时间
        shoppingCart.setCreateTime(LocalDateTime.now());
        //若数据库中有数据则更新若没有则添加
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getGoodsId,shoppingCart.getGoodsId());
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        boolean flag = shoppingCartService.saveOrUpdate(shoppingCart,queryWrapper);
        if (flag != true){
            R.error("购物车添加失败");
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        return R.success(one);

    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车");


        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //获取当前userId
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        //通过userID查询数据
        List<ShoppingCart> records = shoppingCartService.list(queryWrapper);
//        List<ShoppingCart> list = records.stream().map((item) -> {
//            //根据商品id查询商品
//            Long goodsId = item.getGoodsId();
//            LambdaQueryWrapper<Goods> queryWrapper1 = new LambdaQueryWrapper<>();
//            queryWrapper1.eq(Goods::getGoodsId, goodsId);
//            Goods goods = goodsService.getOne(queryWrapper1);
//
//
//            if (goods != null) {
//                item.setGoods(goods);
//            }
//            return item;
//        }).collect(Collectors.toList());

        return R.success(records);

    }

    @PostMapping("/listByIds")
    public R<List<ShoppingCart>> listByIds(@RequestBody List<Long> shoppingIds){
        log.info("查看购物车:{}",shoppingIds);
            List<ShoppingCart> shoppingCarts = shoppingCartService.listByIds(shoppingIds);
            log.info("查询到的数据:{}",shoppingCarts);
            return R.success(shoppingCarts);
    }

    @DeleteMapping("/{shoppingId}")
    public R<String>  deleteById(@PathVariable Long shoppingId){
        boolean b = shoppingCartService.removeById(shoppingId);

        return R.success("删除成功");
    }
}
